package ch.unisi.inf.pfii.teamwhite.risk.graphic.mapEditor;

/**
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public final class EditorFrame extends JFrame{

	private MapNameDialog mapNameDialog;
	
	private FileCreator fileCreator;
	
	private Container contentPane;	
	private String mapName;
	private JPanel imagePanel;
	
	private String arrayName;
	
	private ArrayList<String[]> galaxy;
	private ArrayList<String[]> neighbours;
	
	private int mouseCounter;
	
	private JButton save;
	
	private int x;
	private int y;
	private String selectedPlanet;
	private String neighbourPlanet;
	
	public EditorFrame(final MapNameDialog mapNameDialog){
		this.mapNameDialog = mapNameDialog;
		fileCreator = new FileCreator();
		
		mapName = "";
		mouseCounter = 0;
		x = 0;
		y = 0;
		galaxy = new ArrayList<String[]>();
		neighbours = new ArrayList<String[]>();
		createGalaxy();
		
		contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		contentPane.add(makeNorth(), BorderLayout.NORTH);
		contentPane.add(makeCenter(), BorderLayout.CENTER);
		contentPane.add(makeSouth(), BorderLayout.SOUTH);
		
		setTitle("Map Editor");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		setSize(1024, 768);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		setVisible(true); 
		pack();
	}
	
	private final Component makeNorth() {
		final JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout());
		
		mapName = mapNameDialog.getMapName();
		
		final JLabel northLabel = new JLabel(mapName);
		northPanel.add(northLabel);
		
		return northPanel;
	}
	
	private final Component makeCenter(){
		imagePanel = new JPanel();
		imagePanel.setLayout(null);
		imagePanel.setPreferredSize(new Dimension(1010, 722));
		
		String imageFile = "editorBackground.png";
		ImageIcon image = new ImageIcon(getClass().getResource(imageFile));
		
		final JLabel imageLabel = new JLabel(image);
		imageLabel.setLayout(null);
		imageLabel.setBounds(0, 0, 1010, 722);
		imagePanel.add(imageLabel);
		imagePanel.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {
				if (mouseCounter < 42) {
					String planetName = "Planet" + (mouseCounter + 10);
					x = (int)e.getPoint().getX();
					y = (int)e.getPoint().getY();
					drawPlanet(planetName, x, y);
					writeCoordinates(mouseCounter, x, y);
					mouseCounter++;
				}
			}
		});
		return imagePanel;
	}
	
	private final Component makeSouth() {
		JPanel southPanel = new JPanel();
		southPanel.add(saveAndQuit());
		return southPanel;
	}
	
	private final Component saveAndQuit(){
		final JPanel saveAndQuit = new JPanel();
		saveAndQuit.setLayout(new GridLayout(2, 1));
		
		saveAndQuit.add(makeSave());
		saveAndQuit.add(makeQuit());
		
		return saveAndQuit;
	}
	
	private final Component makeSave(){
		save = new JButton("Save");
		save.setEnabled(false);
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fileCreator.createCoordinatesFile(mapName);
				fileCreator.writeCoordinates(galaxy, x, y);
				fileCreator.createNeighboursFile(mapName);
				fileCreator.writeNeighbours(neighbours);
				System.out.println("salvato");
			}
		});
		return save;
	}
	
	private final Component makeQuit(){
		JButton quit = new JButton("Quit");
		quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quitIfConfirmed();
			}
		});
		return quit;
	}
	
	private final void quitIfConfirmed() {
		ImageIcon icon = new ImageIcon("");
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(EditorFrame.this, "Do you really want to quit?", "Quit?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, icon)) {
            dispose();
            setVisible(false);
        }
    }

	private final void drawPlanet(final String planetName, final int x, final int y) {
		String planetImage = "planet.png";
		ImageIcon image = new ImageIcon(getClass().getResource(planetImage));
		
		final JLabel planet = new JLabel(image);
		planet.setName("Planet" + (mouseCounter + 10));
		
		//TODO
		System.out.println("Planet" + (mouseCounter + 10) + " e' stato creato");
		
		imagePanel.add(planet);
	}

	private final ArrayList<String[]> createGalaxy() {
		for (int i = 10; i < 52; i++) {
			arrayName = i + "";
			String[] arrayName = new String[3];
			arrayName[0] = "Planet" + i;
			galaxy.add(arrayName);
		}
		return galaxy;
	}
	
	private final ArrayList<String[]> createNeighbours() {
		for (int i = 10; i < 52; i++) {
			arrayName = i + "";
			String[] arrayName = new String[4];
			arrayName[0] = "Planet" + i;
			neighbours.add(arrayName);
		}
		return neighbours;
	}
	
	private final void writeCoordinates(final int planetNumber, final int x, final int y) {
		String[] planet = galaxy.get(planetNumber);
		planet[1] = x + "";
		planet[2] = y + "";
	}
	
	private final void writeNeighbours(final int planetNumber, final ArrayList<String> neighbours) {
		String[] planet = galaxy.get(planetNumber);
		int index = 1;
		for (String neighbour : neighbours) {
			planet[index] = neighbour;
			index++;
		}
	}
}
